package com.bootx.controller.api;

import com.bootx.common.Result;
import com.bootx.controller.BaseController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class IndexController extends BaseController {

    @PostMapping("/site")
    public Result site(){
        Map<String,Object> map = new HashMap<>();

        map.put("menus",getMenus());

        return Result.success(map);
    }
    private List<Map<String,Object>> getMenus() {
        List<Map<String,Object>> menus = new ArrayList<>();
        menus = jdbcTemplate.queryForList("select id `key`,name `label`,(select count(id) from menu where menu.parent_id=parent.id) `childCount` from menu parent where parent_id is null order by orders asc ");
        for (Map<String, Object> menu : menus) {
            if(menu.get("childCount")!=null && !menu.get("childCount").toString().equals("0")){
                menu.put("children",jdbcTemplate.queryForList("select id `key`,name `label` from menu where parent_id=? order by orders asc",menu.get("key")));
            }
            menu.remove("childCount");
        }
        return menus;
    }
}
